package com.assistant.market.modules.market.adapter.web;

import com.assistant.core.modules.sys.domain.user.User;
import com.assistant.market.modules.market.adapter.web.model.MarketIssueInfo;
import com.assistant.market.modules.market.app.MarketIssueService;
import com.assistant.market.modules.market.app.cmd.MarketIssueInsertCmd;
import com.assistant.market.modules.market.app.cmd.MarketIssueListCmd;
import com.assistant.util.MessageBodyWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/market")
public class MarketIssueController {

    @Autowired
    private MarketIssueService marketIssueService;

    @GetMapping(value = "/issues")
    public MessageBodyWrapper<List<MarketIssueInfo>> list(MarketIssueListCmd cmd) {
        return MessageBodyWrapper.success(marketIssueService.list(cmd).stream().map(MarketIssueInfo::createBy).collect(Collectors.toList()));
    }

    @PostMapping(value = "/issue")
    public MessageBodyWrapper<Object> insert(@RequestBody @Validated MarketIssueInsertCmd cmd, User user) {
        marketIssueService.insert(cmd);
        return MessageBodyWrapper.success();
    }
}
